<?php

namespace app\platform\controller\admin;

use app\BaseController;

use app\common\logic\TokenLogic;

use app\common\model\account\AccountModel;

use app\common\model\admin\AdminModel;

use app\common\model\TokenModel;

use app\common\model\ShopModel;

use app\common\model\RegisterLogsModel;

use app\common\model\ShopGmvLogsModel;

use app\common\model\OrderModel;

class Main extends BaseController{

    

    public function getCountData(){

        $account = [

            'today' => AccountModel::whereDay('add_time')->count(),

            'month' => AccountModel::whereMonth('add_time')->count(),

            'year'  => AccountModel::whereYear('add_time')->count(),

        ];

        $shop = [

            'today' => ShopModel::whereDay('add_time')->count(),

            'month' => ShopModel::whereMonth('add_time')->count(),

            'year'  => ShopModel::whereYear('add_time')->count(), 

        ];

        $register = [

            'today' => RegisterLogsModel::whereDay('add_time')->count(),

            'month' => RegisterLogsModel::whereMonth('add_time')->count(),

            'year'  => RegisterLogsModel::whereYear('add_time')->count(), 

        ];

        $gmv = [

            'today' => ShopGmvLogsModel::whereDay('add_time')->sum('balance'),

            'month' => ShopGmvLogsModel::whereMonth('add_time')->sum('balance'),

            'year'  => ShopGmvLogsModel::whereYear('add_time')->sum('balance'),

        ];



        $order = [

            'today' => [

                'buy'    => OrderModel::whereDay('add_time')->where([['is_paid','=',1],['type','IN',[1,3]]])->sum('price'),

                'sms'    => OrderModel::whereDay('add_time')->where([['is_paid','=',1],['type','=',2]])->sum('price'),

                'code'   => OrderModel::whereDay('add_time')->where([['is_paid','=',1],['type','=',4]])->sum('price'),

            ],

            'month' => [

                'buy'    => OrderModel::whereMonth('add_time')->where([['is_paid','=',1],['type','IN',[1,3]]])->sum('price'),

                'sms'    => OrderModel::whereMonth('add_time')->where([['is_paid','=',1],['type','=',2]])->sum('price'),

                'code'   => OrderModel::whereMonth('add_time')->where([['is_paid','=',1],['type','=',4]])->sum('price'),

            ],

            'year'  => [

                'buy'    => OrderModel::whereYear('add_time')->where([['is_paid','=',1],['type','IN',[1,3]]])->sum('price'),

                'sms'    => OrderModel::whereYear('add_time')->where([['is_paid','=',1],['type','=',2]])->sum('price'),

                'code'   => OrderModel::whereYear('add_time')->where([['is_paid','=',1],['type','=',4]])->sum('price'),

            ]

        ];

        $return = [

            'account' => $account,

            'shop'    => $shop,

            'register' => $register,

            'gmv'     => $gmv,

            'order'  => $order,

        ];

        return $this->success($return);

    }

    public function getMiniappUseRank(){

        $type = $this->request->param('type');

        if($type == 'year'){

            $count = RegisterLogsModel::whereYear('add_time')->field('shop_id,count(mobile) AS num')->group('shop_id')->count();

            $datas = RegisterLogsModel::whereYear('add_time')->field('shop_id,count(mobile) AS num')->group('shop_id')

                    ->order(['num'=>'desc'])

                    ->page($this->request->page)->limit($this->request->limit)->select()->toArray();

        }else{

            $count = RegisterLogsModel::whereMonth('add_time')->field('shop_id,count(mobile) AS num')->group('shop_id')->count();

            $datas = RegisterLogsModel::whereMonth('add_time')->field('shop_id,count(mobile) AS num')->group('shop_id')

                    ->order(['num'=>'desc'])

                    ->page($this->request->page)->limit($this->request->limit)->select()->toArray();

        }

        $shopIds = [];

        foreach($datas as $val){

           $shopIds[$val['shop_id']] = $val['shop_id'];

        }

        $return = [];

        $shopModel = new ShopModel();

        if(!empty($shopIds)){

            $shop = $shopModel->itemsByIds($shopIds);

            foreach($datas as $val){

                if(isset($shop[$val['shop_id']])){

                    $local = [

                        'shop_id'=> $val['shop_id'],

                        'num'    => $val['num'],

                        'name'  =>  $shop[$val['shop_id']]['name'],

                        'wx_face' => $shop[$val['shop_id']]['wx_face'],

                        'wx_name' => $shop[$val['shop_id']]['wx_name'],

                        'wx_principal_name' => $shop[$val['shop_id']]['wx_principal_name'],

                        'add_time' => date('Y-m-d',$shop[$val['shop_id']]['add_time']),

                    ];

                    $return [] = $local;

                }

            }

        }

        

        return $this->success([

            'count' => $count,

            'datas' => $return

        ]);

        

    }



    public function login()

    {

        $username=$this->request->param('username');

        $password=$this->request->param('password');

        $token = $this->request->param('token');

        if (empty($token)) {

            return $this->error('令牌不存在或者已过期');

        }

        if (null === $datas = TokenModel::where('token', $token)->find()) {

            return $this->error('令牌不存在或者已过期');

        }

        if($datas->type != $this->request->type){

            return $this->error('令牌不存在或者已过期');

        }

        if(empty($username)){

            return $this->error('请输入用户名');

        }

        if(empty($password)){

            return $this->error('请输入密码');

        }

        $where=[

            ['username','=',$username],

            ['is_lock','=',0]

        ];

        if(null === $admin=AdminModel::where($where)->find()){

            return $this->error('用户名或者密码错误');

        }

        if($admin->password != md5($password)){

            return $this->error('用户名或者密码错误');

        }

        $admin->last_time=$this->request->time();

        $admin->code=rand(111111,999999);

        $admin->save();

        $auth_token=TokenLogic::getAdminToken($admin->admin_id,$admin->code);

        return $this->success(['auth_token'=>$auth_token]);

    }

    

    public function getCode()

    {

        //CODE

        TokenModel::where([['expire_time', '<', $this->request->time()],['type','=',$this->request->type]])->delete();

        $code = md5(uniqid());

        $token = new TokenModel();

        $token->token = $code;

        $token->expire_time = $this->request->time() + 600;

        $token->type=$this->request->type;

        $token->save();

        return $this->success([

            'code' => $code,

        ]);

    }



    public function editPwd(){

        $pwd = $this->request->param('pwd');

        $repwd = $this->request->param('repwd');

        if(empty($pwd) || empty($repwd)){

            return $this->error('密码和确认密码不能为空');

        }

        if(strlen($pwd) < 6){

            return $this->error('密码不能小于6位');

        }

        if($pwd != $repwd){

            return $this->error('两次密码输入不一致');

        }

        $this->request->admin->password = md5($pwd);

        $this->request->admin->save();

        return $this->success('设置成功');

    }



    public function layout()

    {

        return $this->success([

            'admin'=>$this->request->admin,

        ]);

    }

}

